<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,
        body {
            background-color: #f2f2fc;
            height: 100vh;
            overflow: hidden;
        }

        * {
            margin: 0;
            padding: 0;
            list-style: none;
            text-decoration: none;
            user-select: none;
        }

        ::-webkit-scrollbar {
            display: none;
        }

        .contentBox {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin-top: 40px;
            gap: 10px;
        }

        .inputBox {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .inputBox>input {
            outline: none;
            border: 2px solid rgba(128, 128, 128, 0.3);
            border-radius: 6px;
            height: 30px;
            width: 320px;
            padding: 0 10px;
        }

        .inputBox>p {
            width: 30px;
            height: 30px;
            font-size: 24px;
            font-weight: 500;
            background-color: #fff;
            color: gray;
            border-radius: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid rgba(128, 128, 128, 0.3);
            cursor: pointer;
        }

        .inputBox>input:focus {
            border: 2px solid orangered;
        }

        ul {
            display: flex;
            flex-direction: column;
            gap: 10px;
            align-items: center;
            overflow-y: auto;
            height: 80vh;
        }

        ul>li {
            display: flex;
            gap: 5px;
            align-items: center;
        }

        ul>li>p {
            background-color: #fff;
            border-radius: 6px;
            padding: 0 10px;
            max-width: 320px;
            word-wrap: break-word;
            overflow-wrap: break-word;
        }

        ul>li>span {
            width: 30px;
            height: 30px;
            font-size: 16px;
            font-weight: 500;
            background-color: #fff;
            color: gray;
            border-radius: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid rgba(128, 128, 128, 0.3);
            cursor: pointer;
        }

        ul>li>span:hover {
            filter: brightness(0.9);
            color: orangered;
        }
    </style>
</head>

<body>
    <div class="contentBox">
        <h3>待办事项</h3>
        <span class="inputBox">
            <input type="text">
            <p>+</p>
        </span>
        <ul></ul>
    </div>

    <script type="module">
        import { getTodoList, addTodo, delTodo } from './api.js';

        function $(ele) {
            return document.querySelector(ele);
        }

        const ipt = $('input');
        const subBtn = $('.inputBox>p');
        const ul = $('ul');

        renderPage();

        function renderPage() {
            getTodoList()
                .then(data => {
                    let dt = data;
                    console.log(dt);
                    ul.innerHTML = dt.reduce((total, item) => {
                        return total += `<li id=${item.id}><p>${item.title}</p><span>×</span></li>`;
                    }, '');
                })
                .catch(err => {
                    console.error('获取数据失败:', err);
                });
        }

        subBtn.onclick = function () {
            let title = ipt.value;
            addTodo(title)
                .then(data => {
                    console.log(data);
                })
                .catch(err => {
                    console.error('获取数据失败:', err);
                });
            renderPage();
            ipt.value = '';
        };

        ul.onclick = async function (e) {
            const deleteBtn = e.target.closest('li > span');
            if (deleteBtn?.textContent.trim() === '×') {
                const id = deleteBtn.parentNode.id;
                try {
                    await delTodo(id);
                    renderPage();
                } catch (err) {
                    console.error('删除失败:', err);
                }
            }
        };
    </script>
</body>

</html>